package processes

import "fmt"

var (
	UsersMgr *UserMgr
	AllUsers *AllUser
)

type UserMgr struct {
	onlineUsers map[int]*UserProcess
}

type AllUser struct {
	Users map[int]*UserProcess
}

func init() {
	UsersMgr = &UserMgr{
		onlineUsers: make(map[int]*UserProcess, 1024),
	}
	AllUsers = &AllUser{
		Users: make(map[int]*UserProcess, 1024),
	}
}

func (u *UserMgr) AddOnlineUser(up *UserProcess) {
	u.onlineUsers[up.UserId] = up
}

func (u *UserMgr) DelOnlineUser(userId int) {
	delete(u.onlineUsers, userId)
}

func (u *UserMgr) GetAllOnlineUser() map[int]*UserProcess {
	return u.onlineUsers
}

func (u *UserMgr) GetOnlineUserById(userId int) (up *UserProcess, err error) {
	up, ok := u.onlineUsers[userId]
	if !ok {
		err = fmt.Errorf("用户%d未在线", userId)
	}
	return
}
